﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EvidenceLib.Case;
using EvidenceService.Core;
using NHibernate;

namespace EvidenceService.Case
{
    /// <summary>
    /// 出入库单据明晰服务
    /// </summary>
    public class StockBillDetailService:BasicService<StockBillDetail>
    {
        public StockBillDetailService(ISession session)
            : base(session)
        {

        }

        public override void SaveOrUpdate(StockBillDetail entity)
        {

            base.SaveOrUpdate(entity);

            //UpdateStorageEvidence(billType, entity.BillId, entity.Storage.Id,entity.Evidence.BillId, entity.Evidence.Id, entity.Qty);
        }

        ///// <summary>
        ///// 更新库存数据
        ///// </summary>
        ///// <param name="billType"></param>
        ///// <param name="billId"></param>
        ///// <param name="storageId"></param>
        ///// <param name="evidenceId"></param>
        ///// <param name="qty"></param>
        //public void UpdateStorageEvidence(BillTypeEnum billType, int billId,int storageId, int caseId,int evidenceId, double qty)
        //{
        //    try
        //    {
        //        double qtyAction = 0D;

        //        if (billType == BillTypeEnum.InStock || billType == BillTypeEnum.InquiryInStock)
        //            qtyAction = qty;
        //        else if (billType == BillTypeEnum.InquiryOutStock || billType == BillTypeEnum.OutStock)
        //            qtyAction = qty * -1;
        //        else
        //            throw new Exception("入库单据类型错误!");

        //        string sql = string.Format("if exists(select 1 from Ev_StorageEvidence where StorageId={0} and EvidenceId={1}) ", storageId, evidenceId);
        //        sql += Environment.NewLine + string.Format("    update Ev_StorageEvidence set qty = qty + {0},LastBillId={1} where StorageId={2} and EvidenceId={3}  ", qty,billId, storageId, evidenceId);
        //        sql += Environment.NewLine + "else";
        //        sql += Environment.NewLine + string.Format("    insert into  Ev_StorageEvidence (StorageId,CaseId,EvidenceId,Qty,LastBillId)Values({0},'{1}',{2},{3},{4})  ", storageId,caseId, evidenceId, qty, billId);

        //        mSession.CreateSQLQuery(sql).ExecuteUpdate();
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }

        //}
    }


    
}
